<?php
class PHZ_ip
{
    /**
     * 通过ip地址进行定位操作
     * @param $user_id
     * @param $ip
     * @param $db_connect
     * @return bool
     */
    public static function __adoptIpgetLocation( $user_id , $ip , $db_connect ){
        //是否当前没有定位，没有定位的情况下；才进行这个操作
        $location = PHZ_Member::__getFieldBy_ID( $user_id , 'location' , $db_connect );
        if( $location != "" && $location != null ){
            return true;
        }
        //获取经纬度
        $location = self::__QueryLocal( $ip );
        if( $location != false ){
            PM_x::__QueryLocal( $user_id , $location[1] , $location[0] , $ip , $db_connect );
        }
        return true;
    }

    /**
     * 通过ip地址获取经纬度
     * @param $ip
     */
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    public static function __QueryLocal( $ip ){
        $host    = "http://iploc.market.alicloudapi.com/v3/ip";
        $method  = "GET";
        $appcode = "714625f2af8a4db0888374aace1a7857";
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $appcode);
        $querys = "ip=". $ip;
        $url = $host . "?" . $querys;
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, false);
        if (1 == strpos("$".$host, "https://"))
        {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        $local_info = curl_exec($curl);
        if( $local_info != "" ){
            $local_info = json_decode( $local_info , true );
            $rectangle  = $local_info['rectangle'];
            if( count( $rectangle ) <= 0 ){
                return false;
            }
            $rectangle = explode(";",$rectangle);
            if( isset( $rectangle[0] ) ){
                $rectangle = explode(",",$rectangle[0]);
                return $rectangle;
            }
        }
        return false;
    }

}